#include "customprogresswidget.h"
#include <QPainter>
#include <QFontMetrics>

CustomProgressWidget::CustomProgressWidget(QWidget *parent) :
    QWidget(parent),
    m_rangeMin(-100),
    m_rangeMax(100),
    m_leftValue(0),
    m_rightValue(0),
    m_unit("")
{
    setRange(m_rangeMin, m_rangeMax);
}

// 设置进度条范围
void CustomProgressWidget::setRange(double min, double max) {
    m_rangeMin = min;
    m_rangeMax = max;
    update(); // 触发重绘
}

// 设置左右进度条的值
void CustomProgressWidget::setMinMax(double leftValue, double rightValue) {
    m_leftValue = leftValue;
    m_rightValue = rightValue;
    update(); // 触发重绘
}

void  CustomProgressWidget::setValue(double value){
    value *= 10000;
    if(value > 0){
        m_rightValue = value;
    }else{
        m_leftValue = value;
    }
    update();
}

// 设置单位
void CustomProgressWidget::setUnit(const QString &unit) {
    m_unit = unit;
    update(); // 触发重绘
}

// 获取进度条范围
double CustomProgressWidget::getRangeMin() const {
    return m_rangeMin;
}

double CustomProgressWidget::getRangeMax() const {
    return m_rangeMax;
}

// 获取左右进度条的值
double CustomProgressWidget::getLeftValue() const {
    return m_leftValue;
}

double CustomProgressWidget::getRightValue() const {
    return m_rightValue;
}

// 获取单位
QString CustomProgressWidget::getUnit() const {
    return m_unit;
}

// 绘制事件
void CustomProgressWidget::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 计算控件的高度和宽度
    int width = this->width();
    int height = this->height()-3;

    // 设置字体
    QFont font = painter.font();
    font.setPointSize(8);
    painter.setFont(font);

    // 绘制顶部文本（单位和范围）
    QString rangeText = QString("P/P");
    QString unitText = m_unit;
    QFontMetrics metrics(font);

    // 计算文本位置
    int textHeight = metrics.height()-2;
    int unitTextWidth = metrics.horizontalAdvance(unitText);
    int rangeTextWidth = metrics.horizontalAdvance(rangeText);

    // 绘制单位文本（右上角）
    painter.drawText(width - unitTextWidth - 10, textHeight, unitText);

    // 绘制范围文本（左上角）
    painter.drawText(10, textHeight, rangeText);

    // 计算进度条区域
    int progressBarTop = textHeight + 5; // 顶部文本下方
    int progressBarHeight = height - progressBarTop + 5; // 留出底部间距

    // 0 点始终在正中间
    int zeroPos = width / 2;

    // 绘制背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(200, 200, 200)); // 灰色背景
    painter.drawRect(0, progressBarTop, width, progressBarHeight);

    // 计算负值和正值的比例
    double negativeRange = static_cast<double>(m_rangeMin); // 负值范围
    double positiveRange = static_cast<double>(m_rangeMax);  // 正值范围

    // 绘制左进度条（负值部分）
    if (m_leftValue < 0) {
        painter.setBrush(QColor(0, 255, 0)); // 红色表示负值
        double progressRatio = static_cast<double>(m_leftValue) / negativeRange; // 负值比例
        int progressWidth = static_cast<int>(zeroPos * progressRatio); // 负值宽度
        painter.drawRect(zeroPos - progressWidth, progressBarTop, progressWidth, progressBarHeight);
    }

    // 绘制右进度条（正值部分）
    if (m_rightValue > 0) {
        painter.setBrush(QColor(0, 255, 0)); // 绿色表示正值
        double progressRatio = static_cast<double>(m_rightValue) / positiveRange; // 正值比例
        int progressWidth = static_cast<int>(zeroPos * progressRatio); // 正值宽度
        painter.drawRect(zeroPos, progressBarTop, progressWidth, progressBarHeight);
    }

    // 绘制 0 点线（始终在正中间）
    painter.setPen(QColor(0, 0, 0)); // 黑色竖线
    painter.drawLine(zeroPos, progressBarTop, zeroPos, progressBarTop + progressBarHeight);

    // 绘制当前值文本
    QString leftValueText = QString::number(m_leftValue);
    QString rightValueText = QString::number(m_rightValue);
    int leftValueTextWidth = metrics.horizontalAdvance(leftValueText);
    int rightValueTextWidth = metrics.horizontalAdvance(rightValueText);
    // 绘制左值文本
    painter.drawText(zeroPos - leftValueTextWidth ,textHeight, leftValueText);
    // 绘制右值文本
    painter.drawText(zeroPos,textHeight, "/"+rightValueText);
}
